﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace UDDL.CodeGeneration.CPP
{
	class CppFile : IDisposable
	{
		internal CppFile(String filename)
		{
			writer = new StreamWriter(filename, false, Encoding.UTF8, 0x10000);
			writer.WriteLine("#pragma once");
		}

		public void Write(string format, params object[] arg)
		{
			Indent();
			writer.Write(format, arg);
		}

		public void WriteLine(string format, params object[] arg)
		{
			Indent();
			writer.WriteLine(format, arg);
			m_unfinishedLine = false;
		}

		public void ChangeIdentation(int c)
		{
			m_indent += c;
			Debug.Assert(m_indent >= 0);
		}

		private void Indent()
		{
			if (!m_unfinishedLine)
			{
				m_unfinishedLine = true;
				for (int i = m_indent; i-- > 0; )
					writer.Write("\t");
			}
		}

		private int m_indent = 0;
		private bool m_unfinishedLine;

		private TextWriter writer;

		#region IDisposable Members
		void IDisposable.Dispose()
		{
			writer.Dispose();
		}
		#endregion
	}
}
